package org.example.util;

import org.apache.commons.lang.StringUtils;

import javax.xml.namespace.QName;
import javax.xml.soap.*;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.Map;

public class XmlUtil {

    public static void main(String[] args) throws Exception {
       /* Map<String, String> map = new HashMap<>();
        map.put("name", "steven");
        map.put("age", "18");
        map.put("sex", "male");
        String center = buildXml(map, "center");
        System.out.println(center);*/
        //String storeId = "1001A110000005E7553Z, 1001A110000003LTJ3XD";
        String storeId = "1001A110000005E7553Z";
        if (StringUtils.isNotBlank(storeId)){
            String[] split = storeId.split(",");
            System.out.println(Arrays.asList(split));
        }
    }
    public static String buildXml(Map<String, String> paramMap, String pub) throws Exception {
        // 检查参数是否为空
        if (null == paramMap) {
            throw new IllegalArgumentException("参数不能为空");
        }
        // 创建SOAP消息工厂
        MessageFactory messageFactory = MessageFactory.newInstance();
        // 创建SOAP消息
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();

        // 添加命名空间声明
        envelope.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
        envelope.addNamespaceDeclaration("pub", "http://pub.itf.ws.nc/PubWs");

        // 创建SOAP主体
        SOAPBody body = envelope.getBody();

        // 创建SOAP主体元素
        QName bodyName = new QName("http://pub.itf.ws.nc/PubWs", pub, "pub");
        SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

        // 创建子元素
        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
            SOAPElement element = bodyElement.addChildElement(entry.getKey());
            element.addTextNode(entry.getValue());
        }

        // 如果需要将SOAP消息转换为字符串
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        soapMessage.writeTo(outputStream);
        return outputStream.toString();
    }
}
